package indzcraft.ui;

import indzcraft.loader.Equipment;
import indzcraft.loader.ItemEquiped;
import indzcraft.loader.Reforge;
import indzcraft.loader.ReforgeDB;
import indzcraft.ranking.EP;
import indzcraft.ranking.ItemCompare;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;

public class ReforgePanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private IndzCraftUI icui = null;
	private int slot;
	private String slotName;
	private ItemEquiped item;
		
	public ReforgePanel(IndzCraftUI ui,int slot){
		super();
		
		this.icui = ui;
		this.slot = slot;
		this.slotName = Slot.possibleSlots[slot].getName();
		this.item = icui.getLoader().getEquipment().get(slotName);
		setLayout(new GridBagLayout());
        
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.HORIZONTAL;
		
        // TODO very dirty quick hack
        /*String tempSlot = this.slotName;
        if(this.slotName.endsWith("1") || this.slotName.endsWith("2")){
        	tempSlot = this.slotName.substring(0, this.slotName.length()-1);
        }else if(this.slotName.equals("mainHand") || this.slotName.equals("offHand")){
        	tempSlot = "weapon";
        }*/
		// load reforges
		ReforgeDB reforgeDB = icui.getLoader().getReforgeDB();
		ReforgeButton eb = null;
		ArrayList<Reforge> items = new ArrayList<Reforge>();
		for ( Reforge elem : reforgeDB.getDB().values() ){
			if(icui.getCalc().hasItemStat(item,"white_hit") && elem.getFrom().equals("Hit Rating"))
				items.add(elem);
			else if(icui.getCalc().hasItemStat(item,"crit") && elem.getFrom().equals("Crit Rating"))
				items.add(elem);
			else if(icui.getCalc().hasItemStat(item,"haste") && elem.getFrom().equals("Haste Rating"))
				items.add(elem);
			else if(icui.getCalc().hasItemStat(item,"mastery") && elem.getFrom().equals("Mastery Rating"))
				items.add(elem);
			else if(icui.getCalc().hasItemStat(item,"exp") && elem.getFrom().equals("Expertise Rating"))
				items.add(elem);
			if(icui.getCalc().hasItemStat(item,"white_hit") && elem.getTo().equals("Hit Rating"))
				items.remove(elem);
			else if(icui.getCalc().hasItemStat(item,"crit") && elem.getTo().equals("Crit Rating"))
				items.remove(elem);
			else if(icui.getCalc().hasItemStat(item,"haste") && elem.getTo().equals("Haste Rating"))
				items.remove(elem);
			else if(icui.getCalc().hasItemStat(item,"mastery") && elem.getTo().equals("Mastery Rating"))
				items.remove(elem);
			else if(icui.getCalc().hasItemStat(item,"exp") && elem.getTo().equals("Expertise Rating"))
				items.remove(elem);
		}
		items.add(null);
		ItemCompare<Reforge> ic = new ItemCompare<Reforge>(icui,item,items);
		ArrayList<EP<Reforge>> itemep = ic.getComperator();
		Iterator<EP<Reforge>> it = itemep.iterator();
		int j = 0;
		double min = 0;
		EP<Reforge> last = itemep.get(itemep.size()-1);
		if(min == 0) min = icui.calc.calcReforgeEP(item,last.getItem());
		double max = 0;
		Reforge curReforge = icui.getLoader().getEquipment().get(this.slotName).getReforge();
		while (it.hasNext()) {
			Reforge reforge = it.next().getItem();
			if(max == 0) max = icui.calc.calcReforgeEP(item,reforge);
			eb = new ReforgeButton(reforge,this.slot);
			
			c.anchor = GridBagConstraints.WEST;
            c.insets = new Insets(0, 0, 0, 7);
            c.gridx = 0; c.gridy = j; c.gridheight = 3; c.gridwidth = 1;
            if(reforge == null)
            	reforge = new Reforge();
            if (curReforge.equals(reforge))
				eb.setSelected(true);
            add(eb, c);
            
            String name = "None";
            if(reforge != null)
            	name = reforge.getFrom() + " -> " + reforge.getTo();
            if (name.length()>35)
                    name = name.substring(0,32)+"...";
            JLabel label = new JLabel(name);
            c.insets = new Insets(0, 0, 0, 0);
            c.gridx = 2; c.gridy = j; c.gridheight = 1;
            add(label, c);
            /*
            String lvl = ""+enchant.toString();
            label = new JLabel(String.format("toString: %s", lvl));
            c.gridx = 2; c.gridy = j+1;
            add(label, c);*/
            
            label = new JLabel(String.format("%.2f", icui.calc.calcReforgeEP(item,reforge)));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j;
            add(label, c);
            
            double diff;
            if (curReforge != null)
                diff = icui.calc.calcReforgeEP(item,reforge) - icui.calc.calcReforgeEP(item,curReforge);
            else
                diff = icui.calc.calcReforgeEP(item,reforge);
            label = new JLabel(String.format("%+.2f", diff));
            label.setHorizontalAlignment(JLabel.RIGHT);
            c.gridx = 3; c.gridy = j+1;
            add(label, c);
            
            // bar
            c.insets = new Insets(5, 0, 0, 0);
            c.gridx = 2; c.gridy = j+2; c.gridwidth = 2;
            // TODO negative values should be seen in the bar
            add(new RatingPanel(icui.calc.calcReforgeEP(item,reforge), (float) max), c);
            
            if (it.hasNext()) {
                JSeparator sep = new JSeparator();
                c.insets = new Insets(3, 0, 3, 0);
                c.gridx = 1; c.gridy = j+3; c.gridwidth = 3;
                add(sep, c);
                j += 1;
            }
            
            j += 3;
		}
		setBorder(new EmptyBorder(new Insets(3,6,6,6)));
        setVisible( true );
	}
	
	public class ReforgeButton extends JRadioButton implements ActionListener {
        
		private static final long serialVersionUID = 1L;
		private int slot;
		private String slotName;
		private Reforge reforge;
        
        public ReforgeButton(Reforge reforge,int slot) {
        	this.slot = slot;
        	this.slotName = Slot.possibleSlots[slot].getName();
    		this.reforge = reforge;
            setFocusable(false);
            addActionListener(this);
            setVisible(true);
            
            setLayout(null);
            
        }

        public void actionPerformed(ActionEvent e) {
        	
        	System.out.println(this.slot);
        	Equipment e1 = icui.getLoader().getEquipment();
        	e1.get(slotName).setReforge(reforge);
        	
        	icui.setItemSlot(this.slot, e1.get(slotName));
        	icui.showReforgePanel(this.slot);
        	icui.showStats();
        }

	}
}

